<template>
  <div class="pb50">
    <el-form
      size="small"
      ref="form"
      :model="form"
      label-width="150px"
      v-if="!loading"
    >
      <!--商品信息-->
      <Info></Info>

      <!--规格-->
      <Spec></Spec>

      <!--其它-->
      <Other></Other>

      <!--提交-->
    </el-form>

    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="onSubmit"
        :loading="loading"
        >提交</el-button
      >
    </div>
  </div>
</template>
<script>
import AdvanceApi from "@/api/advance.js";
import Info from "./part/Info.vue";
import Spec from "./part/Spec.vue";
import Other from "./part/Other.vue";
export default {
  components: {
    /*产品基本信息*/
    Info,
    /*规格*/
    Spec,
    /*其它*/
    Other,
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*表单*/
      form: {
        /*商品ID*/
        productId: 0,
        /*商品状态*/
        status: 10,
        /*商品名称*/
        productName: "",
        /*排序*/
        sort: 100,
        /*规格*/
        producSkuId: 0,
        /*限购数量*/
        limitNum: 1,
        /*定金*/
        money: "",
        /*尾款立减金额*/
        reduceMoney: "",
        /*活动时间*/
        activeTime: [],
        /*商品表格*/
        tableData: [],
      },
    };
  },
  provide: function () {
    return {
      form: this.form,
      type: "add",
    };
  },
  created() {
    if (this.$route.query.productId != null) {
      this.form.productId = this.$route.query.productId;
      this.getData();
    }
  },
  methods: {
    /*获取商品*/
    getData() {
      let self = this;
      AdvanceApi.getProduct(
        {
          productId: self.form.productId,
        },
        true
      ).then((res) => {
        self.form = Object.assign(self.form, res.data);
        // res.data.sku.forEach((item) => {
        //   item.productName = self.form.productName;
        //   item.specType = res.data.specType;
        //   if (res.data.specList) {
        //     res.data.specList.forEach((sitem) => {
        //       if (item.specSkuId == sitem.specSkuId) {
        //         item.specName = sitem.specName;
        //       }
        //     });
        //   }
        //   item.advancePrice = 0;
        //   item.advanceStock = 0;
        //   item.sort = 100;
        //   item.tempProduct = true;
        //   item.isDelete = 0;
        //   item.advanceProductId = 0;
        //   item.advanceProducSkuId = 0;
        //   item.salesInitial = 0;
        // });
        //self.form.tableData = res.data.sku;
        self.loading = false;
      });
    },

    /*格式规格表格数据*/
    creatSpec() {
      let self = this;
      let obj = {
        productName: self.form.productName,
        productId: self.form.productId,
        productPrice: self.form.productPrice,
        stock_num: self.form.productStock,
        producSkuId: self.form.productSku.producSkuId,
        specType: self.form.specType,
        advancePrice: 0,
        advanceStock: 0,
        sort: 100,
        tempProduct: true,
        isDelete: 0,
        advanceProductId: 0,
        advanceProducSkuId: 0,
        salesInitial: 0,
      };
      self.form.tableData.push(obj);
    },

    /*提交表单*/
    onSubmit() {
      let self = this;
      self.$refs.form.validate((valid) => {
        if (valid) {
          let params = {};
          params.productId = self.form.productId;
          params.money = self.form.money;
          params.status = self.form.status;
          params.sort = self.form.sort;
          params.limitNum = self.form.limitNum;
          if (self.form.activeTime && self.form.activeTime.length > 0) {
            params.startTime = self.form.activeTime[0];
            params.endTime = self.form.activeTime[1];
          }
          // params.activeTime = self.form.activeTime;
          params.reduceMoney = self.form.reduceMoney;
          params.specList = self.tableFormet(self.form.tableData);
          self.loading = true;
          AdvanceApi.addProduct(params, true)
            .then((data) => {
              self.loading = false;
              ElMessage({
                message: "恭喜你，添加成功",
                type: "success",
              });
              self.cancelFunc();
            })
            .catch((error) => {
              self.loading = false;
            });
        }
      });
    },

    /*表格数据格式化*/
    tableFormet(list) {
      list.forEach((item) => {
        item.productAttr = item.specName != null ? item.specName : "";
      });
      return list;
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },
  },
};
</script>
<style>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>
